package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.pojo.*;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/statics")
public class StaticsController {


    @Autowired
    CategoryService categoryService;

    @Autowired
    OrderDetailService orderDetailService;

    @Autowired
    DishService dishService;

    @GetMapping("/dishCash")
    public DishCash dishCash(){
        //获取所有的菜系
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Category::getType,1);
        List<Category> list = categoryService.list();
        //Map<String, Integer> map = new HashMap<>();
        //获取各菜系的营业额
        List dishCategoryCash = new ArrayList<>();
        for (Category category : list) {
            QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
            dishQueryWrapper.lambda().eq(Dish::getCategoryId,category.getId());
            List<Dish> dishes = dishService.list(dishQueryWrapper);
            String categoryName = category.getName();
            Integer num = 0;
            for (Dish dish : dishes) {
                QueryWrapper<OrderDetail> wrapper1 = new QueryWrapper<>();
                wrapper1.lambda().eq(OrderDetail::getDishId,dish.getId());
                List<OrderDetail> details = orderDetailService.list(wrapper1);
                for (OrderDetail detail : details) {
                    BigDecimal amount = detail.getAmount();
                    int i = amount.intValue();
                    num+=i;
                }
            }
            if(num>0){
                DishCategoryCash sales = new DishCategoryCash();
                sales.setValue(num);
                sales.setName(categoryName);
                dishCategoryCash.add(sales);
            }
        }
        DishCash dishCash = new DishCash();
        dishCash.setDishCash(dishCategoryCash);
        return dishCash;
    }

}



